package com.ciko.code.fragment;


import android.content.Intent;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.Toast;

import com.ciko.code.adapter.WorkPlaneListAdapter;
import com.ciko.code.base.BaseFragment;
import com.ciko.code.bean.SelAcceptPlanEty;
import com.ciko.code.http.BaseResponse;
import com.ciko.code.http.Req.SelAcceptPlanReq;
import com.ciko.code.views.pullrefresh.PullToRefreshBase;
import com.ciko.code.views.pullrefresh.PullToRefreshListView;
import com.mywork.R;
import com.mywork.activity.WorkPlaneDetailActivity;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by 12154 on 2015/11/10.
 */
public class WorkPlaneSendFragment extends BaseFragment implements AdapterView.OnItemClickListener {

    private List<SelAcceptPlanEty> selAcceptPlanEtyList = new ArrayList<>();

    private WorkPlaneListAdapter workPlaneListAdapter;

    private PullToRefreshListView pullToRefreshListView;
    private ListView lv_workPlane;
    private RelativeLayout rlNoneTipLogo;

    @Override
    protected void initTopBar() {
        unableNav();
    }

    @Override
    protected void initLayout() {
        setContentView(R.layout.fragment_work_plane_send);
    }

    @Override
    protected void initView() {
        pullToRefreshListView = findViewById(R.id.lv_workPlane);
        lv_workPlane = pullToRefreshListView.getRefreshableView();
        rlNoneTipLogo = findViewById(R.id.rl_none_tip_logo);
    }

    @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {
        super.setUserVisibleHint(isVisibleToUser);
        if (isVisibleToUser) {
            //相当于Fragment的onResume
            Log.e("WorkPlaneSendFragment", "onResume");
            doRequestHttp(new SelAcceptPlanReq("2", 0, 10));
        } else {
            //相当于Fragment的onPause
        }
    }

    @Override
    protected void initAdapter() {
        workPlaneListAdapter = new WorkPlaneListAdapter(getContext(), selAcceptPlanEtyList, R.layout.item_list_work_plane);
        lv_workPlane.setAdapter(workPlaneListAdapter);
        pullToRefreshListView.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener<ListView>() {
            @Override
            public void onPullDownToRefresh(PullToRefreshBase<ListView> refreshView) {
                doRequestHttp(new SelAcceptPlanReq("2", 0, 10));
            }

            @Override
            public void onPullUpToRefresh(PullToRefreshBase<ListView> refreshView) {
                doRequestHttp(new SelAcceptPlanReq("2", 0, 10));
            }
        });
    }

    @Override
    protected void initDates() {

    }

    @Override
    protected void initHttpRequest() {
//        doRequestHttp(new SelAcceptPlanReq("2", 0, 10));
    }

    @Override
    protected void initListener() {
        lv_workPlane.setOnItemClickListener(this);
    }

    @Override
    public void onResponseSuccess(BaseResponse<?> response, String tag) {
        super.onResponseSuccess(response, tag);
        pullToRefreshListView.onPullDownRefreshComplete();
        List<SelAcceptPlanEty> selAcceptPlanEtyListResult = (List<SelAcceptPlanEty>) response.getReturnObject();
        selAcceptPlanEtyList.clear();
        selAcceptPlanEtyList.addAll(selAcceptPlanEtyListResult);
        workPlaneListAdapter.notifyDataSetChanged();
        if(selAcceptPlanEtyList.size() == 0)
        {
            pullToRefreshListView.setVisibility(View.GONE);
            rlNoneTipLogo.setVisibility(View.VISIBLE);
        }
    }

    @Override
    public void onResponseFail(String tag,int resultCode) {
        Toast.makeText(getActivity(),"网络错误，请重试",Toast.LENGTH_SHORT).show();
        pullToRefreshListView.onPullDownRefreshComplete();
    }


    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

        SelAcceptPlanEty selAcceptPlanEty = (SelAcceptPlanEty) parent.getAdapter().getItem(position);

        Intent intent = new Intent(getContext(), WorkPlaneDetailActivity.class);
        intent.putExtra("SelAcceptPlanEty", selAcceptPlanEty);
        startActivity(intent);

    }

    @Override
    public void onResume() {
        super.onResume();
    }
}
